<?php
/* This file is part of LlamaSlayers Ajax Chat
 *
 * The source code is released under the GNU GPL free software license, version 3.
 * Updates can be found at http://llamaslayers-ajax-chat.googlecode.com/
 */

header( 'Content-Type: text/javascript' );

session_start();

if ( !empty( $_SESSION['name'] ) ) {
	if ( empty( $_SESSION['authenticated'] ) ) {
		die( 'name="' . addslashes( $_SESSION['name'] ) . '";$(function(){$("#right").append("<p>Your username has not been registered. This means that anyone can use it. Why don\'t you <a href=\"login.html#' . addslashes( trim( $_SESSION['name'] ) ) . '\">register it now</a>?</p>")})' );
	} else {
		$user = file( dirname( __FILE__ ) . '/db/user/' . $_SESSION['authenticated'] . '.txt' );

		if ( (int)$user[1] > 0 )
			die( 'name="' . addslashes( $_SESSION['name'] ) . '";$(function(){$("#right").append("<p>You are an administrator. You can use the <a href=\"admin.php\">administrative backend</a>.</p><p>Your icon is currently <a href=\"icons/' . rtrim( $user[2] ) . '_large.png\"><img border=\"0\" src=\"icons/' . rtrim( $user[2] ) . '_small.png\" height=\"15\" width=\"15\"/></a>. <a href=\"icon.html\">Click here to change it</a>.</p>")})' );
		else
			die( 'name="' . addslashes( $_SESSION['name'] ) . '";$(function(){$("#right").append("<p>Your icon is currently <a href=\"icons/' . rtrim( $user[2] ) . '_large.png\"><img border=\"0\" src=\"icons/' . rtrim( $user[2] ) . '_small.png\" height=\"15\" width=\"15\"/></a>. <a href=\"icon.html\">Click here to change it</a>.</p>")})' );
	}
}

if ( !empty( $_GET['name'] ) && trim( $_GET['name'] ) ) {
	require_once dirname( __FILE__ ) . '/config.php';

	foreach ( str_split( $_GET['name'] ) as $n ) {
		if ( in_array( $n, str_split( 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123546789-_ ~`' ) ) )
			continue;
		die( 'name=prompt("The name you entered was invalid. Please try a different name. This will be used as your handle in the chat.","");$.getScript("name.php?name="+encodeURIComponent(name));' );
	}

	if ( in_array( trim( strtolower( $_GET['name'] ) ), (array)$block_username_registration ) || strlen( trim( strtolower( $_GET['name'] ) ) ) < MIN_USERNAME_LENGTH )
		die( 'name=prompt("The name you entered was invalid. Please try a different name. This will be used as your handle in the chat.","");$.getScript("name.php?name="+encodeURIComponent(name));' );

	if ( file_exists( dirname( __FILE__ ) . '/db/user/' . trim( strtolower( $_GET['name'] ) ) . '.txt' ) )
		die( 'if(confirm("That username is reserved.\n\nClick OK and you will be prompted for a password. Click cancel to select a different username.")){promptPassword("' . trim( $_GET['name'] ) . '")}else{name=prompt("What is your name? This will be used as your handle in the chat.","");$.getScript("name.php?name="+encodeURIComponent(name))}' );

	$_SESSION['name'] = trim( $_GET['name'] );
	die( 'location.reload()' );
}

?>name=prompt('What is your name? This will be used as your handle in the chat.','');$.getScript('name.php?name='+encodeURIComponent(name));